package com.jtlrm.ckd.mvp.view.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.base.sdk.util.EditInputFilter;
import com.base.sdk.util.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.entity.ShiWuEntity;
import com.jtlrm.ckd.util.ContactList.ShiWuSelectAdapter;

import java.util.ArrayList;
import java.util.List;

public class ShiWuSearchAdapter extends BaseAdapter {

    Context context;
    private int index = -1;
    List<ShiWuEntity> shiWuEntities;
    LayoutInflater layoutInflater;

    public ShiWuSearchAdapter(Context context) {
        this.context = context;
        shiWuEntities = new ArrayList<>();
        layoutInflater = LayoutInflater.from(context);
    }

    public void replaceData(List<ShiWuEntity> shiWuEntities) {
        this.shiWuEntities.clear();
        this.shiWuEntities = shiWuEntities;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return shiWuEntities.size();
    }

    @Override
    public Object getItem(int position) {
        return shiWuEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return shiWuEntities.get(position).getId();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        ShiWuEntity shiWuEntity = (ShiWuEntity) getItem(position);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.shiwu_select_item, null);
            holder.nameView = convertView.findViewById(R.id.name);
            holder.headerView = convertView.findViewById(R.id.header);
            holder.gramView = convertView.findViewById(R.id.gram);
            holder.jianView = convertView.findViewById(R.id.jian);
            holder.addView = convertView.findViewById(R.id.add);
            holder.gramView.setTag(position);
            holder.gramView.addTextChangedListener(new MyTextWather(holder.gramView));
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            holder.gramView.setTag(position);
        }

        Double gram = shiWuEntity.getGram();
        if (gram != -1) {
            holder.gramView.setText(gram + "");
        } else {
            holder.gramView.setText("");
        }
        holder.gramView.setTag(position);
        holder.gramView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    index = (int) v.getTag();
                }
                return false;
            }
        });
        if (index != -1 && index == position) {
            // 如果当前的行下标和点击事件中保存的index一致，手动为EditText设置焦点。
            holder.gramView.requestFocus();
        } else {
            holder.gramView.clearFocus();
        }
        InputFilter[] filters = {new EditInputFilter(context)};
        holder.gramView.setFilters(filters);

        holder.nameView.setText(shiWuEntity.getNameStr());
        holder.jianView.setTag(position);
        holder.addView.setTag(position);
        holder.jianView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int p = (int) v.getTag();
                ShiWuEntity entity = shiWuEntities.get(p);
                Double gram = entity.getGram();
                if (gram > 1) {
                    Double current = --gram;
                    entity.setCalcium(current);
                    notifyDataSetChanged();
                } else {
                    entity.setGram(0.0);
                    notifyDataSetChanged();
                }
            }
        });
        holder.addView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int p = (int) v.getTag();
                ShiWuEntity entity = shiWuEntities.get(p);
                Double gram = entity.getGram();
                if (gram < 2000) {
                    Double current = ++gram;
                    entity.setGram(current);
                    notifyDataSetChanged();
                }
            }
        });

        return convertView;
    }

    class MyTextWather implements TextWatcher {
        EditText editText;

        public MyTextWather(EditText editText) {
            this.editText = editText;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            int position = (int) editText.getTag();
            ShiWuEntity entity = shiWuEntities.get(position);
            String content = s.toString();
            if (!TextUtils.isEmpty(content) && content.length() < 10) {
                Double value = Double.parseDouble(content);
                if (value > 2000) {
                    entity.setGram(2000.0);
                    ToastUtil.TextToast(context, "最大值只能为2000");
                } else if (value < 0) {
                    ToastUtil.TextToast(context, "最小值为0");
                    entity.setGram(0.0);
                } else {
                    entity.setGram(value);
                }
            }
        }
    }


    private static class ViewHolder {
        TextView nameView;
        TextView headerView;
        EditText gramView;
        ImageView jianView;
        ImageView addView;
    }
}

